package com.fy.proj5_permission_transaction_cache.controller;

import com.fy.proj5_permission_transaction_cache.model.Permission;
import com.fy.proj5_permission_transaction_cache.service.PermissionService;
import com.fy.proj5_permission_transaction_cache.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 权限管理控制器
 * 提供权限相关的REST API接口
 */
@RestController
@RequestMapping("/api/permissions")
public class PermissionController {
    
    @Autowired
    private PermissionService permissionService;
    
    /**
     * 获取所有权限
     * @return 权限列表
     */
    @GetMapping
    public Map<String, Object> getAllPermissions() {
        try {
            List<Permission> permissions = permissionService.findAllPermissions();
            return ResponseUtil.success(permissions);
        } catch (Exception e) {
            return ResponseUtil.error("获取权限列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除权限
     * @param id 权限ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> deletePermission(@PathVariable Long id) {
        try {
            boolean success = permissionService.deletePermission(id);
            if (success) {
                return ResponseUtil.success("权限删除成功");
            } else {
                return ResponseUtil.error("权限删除失败");
            }
        } catch (Exception e) {
            return ResponseUtil.error("删除权限失败: " + e.getMessage());
        }
    }
}